package com.suian.spring.service.impl;

import com.suian.spring.Application;
import com.suian.spring.service.IUserService;
import com.suian.spring.service.invocation.UserServiceCustomInvocationHandler;
import com.suian.spring.entity.User;
import com.suian.spring.mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.lang.reflect.*;
import java.util.List;

@SpringBootTest(classes = Application.class)
class IUserServiceImplRunner {

    @Autowired
    private IUserService userService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    SqlSessionFactory sqlSessionFactory;

    @Test
    public void testSpring() {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        context.getBean("ss");

    }


    @Test
    void getAllUsers() {
        List<User> allUsers = userService.getAllUsers();
        System.out.println(allUsers);
    }

    @Test
    void testInvoker() {
//        System.out.println(userMapper.getAllUsers());
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            // 执行 SQL 操作
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            List<User> allUsers = userMapper.getAllUsers();
            System.out.println(allUsers);
        }



    }
    @Test
    void reflection() {
        //获取class对象
        Class<IUserServiceImpl> iUserServiceClass = IUserServiceImpl.class;
        //获取构造器
        Constructor<?>[] constructors = iUserServiceClass.getConstructors();
        for (Constructor<?> constructor : constructors) {
            try {
                System.out.println(constructor.newInstance());
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Test
    void invocationDemo() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        IUserServiceImpl target = new IUserServiceImpl();
        //关键代码
//        IUserService userService = (IUserService) Proxy.newProxyInstance(IUserServiceImpl.class.getClassLoader(), IUserServiceImpl.class.getInterfaces(), new UserServiceCustomInvocationHandler(iUserService));
//        List<User> allUsers = userService.getAllUsers();
//        System.out.println(allUsers);
        Class<?> proxyClass = Proxy.getProxyClass(IUserServiceImpl.class.getClassLoader(), IUserServiceImpl.class.getInterfaces());
        Constructor<?> constructor = proxyClass.getConstructor(InvocationHandler.class);
        IUserService userService1 = (IUserService) constructor.newInstance(new UserServiceCustomInvocationHandler(target));
        List<User> allUsers = userService1.getAllUsers();
        System.out.println(allUsers);
    }
}